// ActionScript file
import com.mit.web.business.proxy.PurchaseProxy;
import com.mit.web.business.remote.IInvokeResponder;
import com.mit.web.config.Constants;
import com.mit.web.event.PopWinCloseEvent;
import com.mit.web.util.DataFormatUtil;
import com.mit.web.view.purchase.PODetailEditWindow;
import com.mit.web.vo.purchase.POItemVO;
import com.mit.web.vo.purchase.POVO;

import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.ListEvent;
import mx.rpc.events.ResultEvent;

import org.flexunit.runner.Result;



[Bindabled] private var _order:POVO;
[Bindabled] private var _itemList:ArrayCollection;
private var _proxy:PurchaseProxy = PurchaseProxy.getInstance();

public function set order(o:POVO):void{
	_order = o;
}

private function init(event:FlexEvent):void{
	this.title = _order.code;
	if(_order.state == Constants.PO_STATE_NEW)
		this.currentState = "pendingapprove";
	else if(_order.state == Constants.PO_STATE_APPR)
		this.currentState = "approved";
	else if(_order.state == Constants.PO_STATE_COMPLETE)
		this.currentState = "complete";
}

private function loadMergedItem(event:FlexEvent){
	loadItems();
}

private function loadItems():void{
	var resp:IInvokeResponder = _proxy.getMergedPOItems(_order.id);
	resp.addResultListener(loadItemsHandler);
	resp.addFaultListener(exceptionHandler);
}

private function loadItemsHandler(event:ResultEvent):void{
	_itemList = event.result as ArrayCollection;
	var totalAmt:Number = 0;
	for each(var item:POItemVO in _itemList){
		totalAmt += item.amt;
	}
	//Recalculate the total amt after items updated!
	txt_amt.text = DataFormatUtil.toCurrencyStr(totalAmt);
	dg.dataProvider = _itemList;
	dg.validateNow();
}

private function doDelete():void{
	var resp:IInvokeResponder = _proxy.deletePO(_order.id);
	resp.addResultListener(deletePOHandler);
	resp.addFaultListener(exceptionHandler);
}

private function deletePOHandler(event:ResultEvent):void{
	var e:PopWinCloseEvent = new PopWinCloseEvent(PopWinCloseEvent.DELETED);	
	e.msg = ResourceManager.getInstance()
		.getString(Constants.BUNDLE_COMMON,"info.common.deleted",[_order.code]);
	this.dispatchEvent(e);
	closeWindow(this);	
}

/**
 * 收货完成:生成应付款
 * */
private function doComplete(event:MouseEvent):void{
	Alert.show("确认后，系统将根据收货数量添加应付款记录，你确定该采购订单可以结束了吗？",
		ResourceManager.getInstance().getString(Constants.BUNDLE_COMMON,"label.message.info"),
		Alert.YES|Alert.CANCEL, null	, confirmCompleteHandler,null,Alert.CANCEL);
}

private function confirmCompleteHandler(event:CloseEvent):void{
	if(event.detail == Alert.YES){
		//BUG:不是传的VO，后台无法触发并发锁的检查！！！
		var resp:IInvokeResponder = _proxy.updatePOState(_order.id, Constants.PO_STATE_COMPLETE);
		resp.addResultListener(completeHandler);
		resp.addFaultListener(exceptionHandler);
	}
}

private function completeHandler(event:ResultEvent):void{
	_order.state = Constants.PO_STATE_COMPLETE;
	var e:PopWinCloseEvent = new PopWinCloseEvent(PopWinCloseEvent.SAVED);	
	e.msg = ResourceManager.getInstance()
		.getString(Constants.BUNDLE_COMMON,"info.common.stateUpdate",[_order.code]);
	this.dispatchEvent(e);
	closeWindow(this);	
}

private function doApprove():void{
	//BUG:不是传的VO，后台无法触发并发锁的检查！！！
	var resp:IInvokeResponder = _proxy.updatePOState(_order.id, Constants.PO_STATE_APPR);
	resp.addResultListener(approveHandler);
	resp.addFaultListener(exceptionHandler);
}

private function approveHandler(event:ResultEvent):void{
	_order.state = Constants.PO_STATE_APPR;
	var e:PopWinCloseEvent = new PopWinCloseEvent(PopWinCloseEvent.APPROVED);	
	e.msg = ResourceManager.getInstance()
		.getString(Constants.BUNDLE_COMMON,"info.common.approved",[_order.code]);
	this.dispatchEvent(e);
	closeWindow(this);	
}	

private function doItemEdit(event:ListEvent):void{
	var poItem:POItemVO = _itemList[event.rowIndex];
	var window:PODetailEditWindow = new PODetailEditWindow();
	window.addEventListener(PopWinCloseEvent.SAVED, updateRecordHandler);
	window.poItem = poItem;
	window.supplierName = _order.supplierName;
	window.itemEditable =( _order.state == Constants.PO_STATE_NEW);
	PopUpManager.addPopUp(window,this.owner,true);
	PopUpManager.centerPopUp(window);
}

private function updateRecordHandler(event:PopWinCloseEvent):void{
	showMessageTip(event.msg, dg);
	loadItems();
}

private function doSave(event:MouseEvent):void{
	//TODO
}